package com.magic.easyexcel;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.support.ExcelTypeEnum;

/**
 * @author laoma
 * @create 2021-06-06 10:40
 */
public class Test {
    public static void main(String[] args) {
        // 读取文件
        // 创建excelReaderBuilder实例
        ExcelReaderBuilder readerBuilder = EasyExcel.read();
        // 获取文件对象
        readerBuilder.file("F:\\java-workspace\\malearn\\easyexcel\\listener.xlsx");
        // 指定sheet
        readerBuilder.sheet("listener");
        // 自动关闭输入流
        readerBuilder.autoCloseStream(true);
        // 设置excel文件的格式
        readerBuilder.excelType(ExcelTypeEnum.XLSX);
        // 设置监听器进行数据的解析
        readerBuilder.registerReadListener(new AnalysisEventListener() {
            @Override
            public void invoke(Object o, AnalysisContext analysisContext) {
                // 一行数据读取完成后的回调
                System.out.println(o);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                // 通知文件读取完毕
                System.out.println("数据读取完毕！");
            }
        });
        // 构建读取器
        ExcelReader reader = readerBuilder.build();
        // 读取数据
        reader.readAll();
        // 读取完毕
        reader.finish();
    }
}
